home *** CD-ROM | disk | FTP | other *** search
/ Mac Power 1997 December / MACPOWER-1997-12.ISO.7z / MACPOWER-1997-12.ISO / AMUG / PROGRAMMING / Raven 1.2.sit / Raven 1.2 / Source / Foundation / Common / ZExceptions.h < prev    next >
Text File  |  1997-08-16  |  6KB  |  201 lines

  1. /*
  2.  *  File:       ZExceptions.h
  3.  *  Summary:       ANSI derived exception classes.
  4.  *  Written by: Jesse Jones
  5.  *
  6.  *  Copyright ゥ 1996-1997 Jesse Jones. 
  7.  *    For conditions of distribution and use, see copyright notice in ZTypes.h  
  8.  *
  9.  *    Classes:    TBaseException                      - Base exception class
  10.  *                    TLogicException                  - Errors that could have been caught before running the program.
  11.  *                        TDomainException          - A function's argument is outside the domain of the function.
  12.  *                        TInvalidArgumentException - An argument was completely bogus.
  13.  *                        TLengthException          - An object was asked to grow past a hard-wired limit.
  14.  *                        TAssertException          - Exception thrown by ASSERT and VERIFY (if ASSERTS_THROW is true).
  15.  *                    TRuntimeException              - Errors that can only be caught at runtime.
  16.  *                        TRangeException              - A function tried to produce a result that was outside its range.
  17.  *                        TOverflowException          - An arithmatic overflow occured.
  18.  *                        TSystemException          - A system error occured.
  19.  *                        TCancelException          - The user has canceled an operation.
  20.  *                    TMemoryException              - Not enough memory for operator new or other memory request.
  21.  *                        TSysMemoryException          - Request for system memory failed (ie anything but operator new).
  22.  *
  23.  *  Change History (most recent first):    
  24.  *
  25.  *         <2>     8/16/97    JDJ        Uses OSStatus instead of OSErr. (OSStatus is a long).
  26.  *         <1>     1/13/96    JDJ        Created
  27.  */
  28.  
  29. #pragma once
  30.  
  31. #include <String>
  32.  
  33. #include <ZTypes.h>
  34.  
  35.  
  36. // ===================================================================================
  37. //    ANSI Exception Classes
  38. // ===================================================================================
  39. #if HAS_ANSI_EXCEPTIONS
  40. typedef exception TBaseException;
  41.  
  42. typedef logic_error TLogicException;
  43. typedef domain_error TDomainException;
  44. typedef invalid_argument TInvalidArgumentException;
  45. typedef length_error TLengthException;
  46.  
  47. typedef runtime_error TRuntimeException;
  48. typedef range_error TRangeException;
  49. typedef overflow_error TOverflowException;
  50.  
  51. // ・・ For some stupid reason bad_alloc doesn't descend from runtime_error.
  52. // ・・ Hopefully this will be fixed so we can make TSysMemoryException
  53. // ・・ハdescend from TRuntimeException.
  54. typedef bad_alloc TMemoryException;            
  55.  
  56. #else    // HAS_ANSI_EXCEPTIONS
  57.  
  58. class TBaseException { 
  59.  
  60. public: 
  61.     virtual             ~TBaseException();
  62.     
  63.                         TBaseException(const string& mesg); 
  64.     
  65.     virtual const char* what() const; 
  66.     
  67. protected:
  68.     char    mText[256];            
  69. };
  70.  
  71. class TLogicException : public TBaseException { 
  72. public: 
  73.                         TLogicException(const string& mesg) : TBaseException(mesg) {}
  74. };
  75.  
  76.  
  77. class TDomainException : public TLogicException { 
  78. public: 
  79.                         TDomainException(const string& mesg) : TLogicException(mesg) {}
  80. };
  81.  
  82. class TInvalidArgumentException : public TLogicException { 
  83. public: 
  84.                         TInvalidArgumentException(const string& mesg) : TLogicException(mesg) {}
  85. };
  86.  
  87. class TLengthException : public TLogicException { 
  88. public: 
  89.                         TLengthException(const string& mesg) : TLogicException(mesg) {}
  90. };
  91.  
  92.  
  93. class TRuntimeException : public TBaseException { 
  94. public: 
  95.                         TRuntimeException(const string& mesg) : TBaseException(mesg) {}
  96. };
  97.  
  98. class TRangeException : public TRuntimeException { 
  99. public: 
  100.                         TRangeException(const string& mesg) : TRuntimeException(mesg) {}
  101. };
  102.  
  103. class TOverflowException : public TRuntimeException { 
  104. public: 
  105.                         TOverflowException(const string& mesg) : TRuntimeException(mesg) {}
  106. };
  107.  
  108.  
  109. class TMemoryException : public TBaseException { 
  110. public: 
  111.                         TMemoryException() : TBaseException("Not enough memory (object heap)") {}
  112. };
  113.  
  114. #endif    // HAS_ANSI_EXCEPTIONS
  115.  
  116.  
  117. // ===================================================================================
  118. //    PowerPlant Support
  119. // ===================================================================================
  120. #ifdef __PowerPlant__
  121. class ExceptionCode { 
  122. public: 
  123.     virtual             ~ExceptionCode()                {}
  124.     
  125.                         ExceptionCode(long errCode)        {mErrorCode = errCode;}
  126.     
  127.             ExceptionCode& operator=(long err)            {mErrorCode = err; return *this;}
  128.                         
  129.                         operator long() const            {return mErrorCode;}    
  130. protected:
  131.     long    mErrorCode;    
  132. };
  133.  
  134.  
  135. class TSysMemoryException : public TMemoryException, public ExceptionCode { 
  136. public: 
  137.                         TSysMemoryException() : ExceptionCode(-108) {}
  138. };
  139.  
  140.  
  141. class TSystemException : public TRuntimeException, public ExceptionCode { 
  142. public: 
  143.                         TSystemException(OSStatus err, const string& mesg) : TRuntimeException(mesg), ExceptionCode(err) {mError = err;}
  144. public:
  145.     OSStatus    mError;
  146. };
  147. #endif
  148.  
  149.  
  150. // ===================================================================================
  151. //    Raven Exception Classes
  152. // ===================================================================================
  153. #ifndef __PowerPlant__
  154. class TSysMemoryException : public TMemoryException { 
  155. public: 
  156.                         TSysMemoryException()              {}
  157. };
  158.  
  159.  
  160. class TSystemException : public TRuntimeException { 
  161. public: 
  162.                         TSystemException(OSStatus err, const string& mesg) : TRuntimeException(mesg) {mError = err;}
  163. public:
  164.     OSStatus    mError;
  165. };
  166. #endif
  167.  
  168.  
  169. #if ASSERTS_THROW
  170. class TAssertException : public TLogicException { 
  171. public: 
  172.                         TAssertException(const string& mesg) : TLogicException(mesg) {}
  173. };
  174. #endif    
  175.  
  176.  
  177. class TCancelException : public TRuntimeException { 
  178. public: 
  179.                         TCancelException(const string& mesg) : TRuntimeException(mesg) {}
  180. };
  181.  
  182.  
  183. // ===================================================================================
  184. //    Functions
  185. // ===================================================================================
  186. void         ThrowIfNil(const void* ptr);
  187.             
  188. void         ThrowOSErr(OSStatus err);        
  189.  
  190. void         ThrowIfOSErr(OSStatus err);        
  191.  
  192. void         ThrowIfResError();
  193.  
  194. void         ThrowIfMemError();    
  195.  
  196. void         ThrowIfResFail(const void* ptr);                                        
  197.     
  198. void         ThrowIfMemFail(const void* ptr);
  199.             
  200. void         ThrowIfQDError();
  201.